#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>


int read_sysfs_float(const char *device, const char *filename, float *val)
{
	int ret = 0;
	FILE  *sysfsfp;
	char temp[128]; 

	memset(temp, '0', 128);

	ret = sprintf(temp, "/sys/bus/iio/devices/%s/%s", device, filename);
	if (ret < 0)
		goto error;

	sysfsfp = fopen(temp, "r");
	if (!sysfsfp) {
		ret = -errno;
		goto error;
	}

	errno = 0;
	if (fscanf(sysfsfp, "%f\n", val) != 1) {
		ret = errno ? -errno : -ENODATA;
		if (fclose(sysfsfp))
			perror("read_sysfs_float(): Failed to close dir");

		goto error;
	}

	if (fclose(sysfsfp))
		ret = -errno;

error:

	return ret;
}


int read_sysfs_int(const char *device, const char *filename, int *val)
{
	int ret = 0;
	FILE  *sysfsfp;
	char temp[128]; 

	memset(temp, '0', 128);

	ret = sprintf(temp, "/sys/bus/iio/devices/%s/%s", device, filename);
	if (ret < 0)
		goto error;

	sysfsfp = fopen(temp, "r");
	if (!sysfsfp) {
		ret = -errno;
		goto error;
	}

	errno = 0;
	if (fscanf(sysfsfp, "%d\n", val) != 1) {
		ret = errno ? -errno : -ENODATA;
		if (fclose(sysfsfp))
			perror("read_sysfs_float(): Failed to close dir");

		goto error;
	}

	if (fclose(sysfsfp))
		ret = -errno;

error:

	return ret;
}

void led13_On(){
    char chr;
	int  fd;
	int  fd3;
	fd = open("/sys/class/leds/led1/brightness", O_RDWR);
	fd3 = open("/sys/class/leds/led3/brightness", O_RDWR);
	perror("open");
 
	chr = '1';
	write(fd, &chr, 1 );
	write(fd3, &chr, 1 );
	sleep(1);
	printf("led1 and led3 is on\n");
	close(fd);
	close(fd3);
}

void led23_On(){
    char chr;
	int  fd2;
	int  fd3;
	fd2 = open("/sys/class/leds/led2/brightness", O_RDWR);
	fd3 = open("/sys/class/leds/led3/brightness", O_RDWR);
	perror("open");
 
	chr = '1';
	write(fd2, &chr, 1 );
	write(fd3, &chr, 1 );
	sleep(1);
	printf("led2 and led3 is on\n");
	close(fd2);
	close(fd3);
}

void led3_off(){
    char chr;
	int  fd;
	fd = open("/sys/class/leds/led3/brightness", O_RDWR);
	perror("open");
 
	chr = '0';
	write(fd, &chr, 1 );
	sleep(1);
	printf("led3 is off\n");
    close(fd);
}
void led12_offandled3_On(){
    char chr;
	int  fd1;
	int  fd2;
    int  fd3;
	fd1 = open("/sys/class/leds/led1/brightness", O_RDWR);
    fd2 = open("/sys/class/leds/led2/brightness", O_RDWR);
	fd3 = open("/sys/class/leds/led3/brightness", O_RDWR);
	perror("open");

	chr = '0';
	write(fd1,&chr,1);
	write(fd2,&chr,1);
	sleep(1);
	printf("led1 and led2 is off\n");

	chr = '1';
    write(fd3,&chr,1);
	sleep(1);
	printf("led3 is On\n");
    close(fd1);
	close(fd2);
	close(fd3);
}

void led3_off(){
  char chr;
  int fd;
  fd = open("/sys/class/leds/led3/brightness", O_RDWR);
  perror("open");
  
  chr = '0';
  write(fd,&chr,1);
  sleep(1);
  close(fd);

}
int main(int argc, char *argv[])
{
	int temp_raw = 0;
	int temp_offset = 0;
	float temp_scale = 0;
    float temp = 0;   // 温度

	int hum_raw = 0;
	int hum_offset = 0;
	float hum_scale = 0;
    float hum = 0;   // 湿度
  


	if (argc != 2) {
		printf("usage: %s <iio:device[n]>\n", argv[0]);
		return 1;
	}


	while(1) {
		/*read temp data*/
		read_sysfs_int(argv[1], "in_temp_raw", &temp_raw);
		read_sysfs_int(argv[1], "in_temp_offset", &temp_offset);
		read_sysfs_float(argv[1], "in_temp_scale", &temp_scale);
       float temp = (temp_raw + temp_offset) * temp_scale / 1000 ;  
	   // 温度大于24 led1亮
        if (temp < 24)
		{
		   led13_On();
		}

		// 温度大于28 led2 亮
		if(temp >28){
			led23_On();
		}

		// 温度正常
		if(temp >=24 && temp <=28){
			led12_offandled3_On();
		}
        // 温度大于35 led3 灭
		if(temp >35){
           
		}

		printf("temperature = %.2f\n",temp);
 
		read_sysfs_int(argv[1], "in_humidityrelative_raw", &hum_raw);
		read_sysfs_int(argv[1], "in_humidityrelative_offset", &hum_offset);
		read_sysfs_float(argv[1], "in_humidityrelative_scale", &hum_scale);
        float hum = (hum_raw + hum_offset) * hum_scale / 1000；

		 printf("humidity = %.2f%%\n", hum);
		
		sleep(5);
	}

	return 0;
}
